package com.zhudeshuai.zhudemolist.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.zhu.zhuCore.utils.BitmapUtils;
import com.zhu.zhuCore.utils.ContextProvider;
import com.zhu.zhuCore.utils.FileUtil;
import com.zhu.zhuCore.utils.SelectPhotoUtil;
import com.zhu.zhuCore.utils.ToastUtil;
import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.base.BaseActivity;

import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * 图片压缩处理
 *
 * 优化
 *
 * @author zhudeshuai
 * @since 17/12/5
 */
public class ImageScaleHandleActivity extends BaseActivity {

	private ImageView mImageViewTarget;
	private Uri mUri;

	private ImageView mImageViewResultOld;
	private ImageView mImageViewResultNew;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_scale_handle);
		mImageViewTarget = (ImageView) findViewById(R.id.iv_target);
		mImageViewResultOld = (ImageView) findViewById(R.id.iv_result_old);
		mImageViewResultNew = (ImageView) findViewById(R.id.iv_result_new);
	}

	public static void open(Activity ac) {
		open(ac, ImageScaleHandleActivity.class);
	}

	public void selectPic(View view) {
		SelectPhotoUtil.getInstance().showPictureSelected(this, new SelectPhotoUtil.IPhotoSelected() {
			@Override
			public void onPhotoSelected(Uri uri) {
				mUri = uri;
				int bitmapDegree = BitmapUtils.getBitmapDegree(FileUtil.getPath(ImageScaleHandleActivity.this, uri));
				ToastUtil.show("bitmapDegree:" + bitmapDegree);
				mImageViewTarget.setImageBitmap(BitmapUtils.getBitmap(uri));

			}
		});
	}


	/**
	 * 将图片最大边等比缩放至给定值
	 *
	 * @param uri      原始图片本地Uri
	 * @param maxValue 宽或者高的最大值
	 * @return
	 */
	public static Bitmap maxWHBitmap(Uri uri, int maxValue) {

		//判断图片是否被旋转，三星拍照问题
		String path = FileUtil.getPath(ContextProvider.getApplicationContext(), uri);
		BitmapUtils.changeBitmapDegree(path, BitmapUtils.getBitmapDegree(path));

		ContentResolver contentResolver = ContextProvider.getApplicationContext().getContentResolver();
		InputStream inputStream = null;
		try {
			inputStream = new BufferedInputStream(contentResolver.openInputStream(uri));
			inputStream.mark(inputStream.available());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (inputStream != null) {

			//获取图片宽高，计算出缩放比例
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(inputStream, null, options);
			int width = options.outWidth;
			int height = options.outHeight;

			//载入原图
			options.inJustDecodeBounds = false;
			Bitmap bitmap = BitmapFactory.decodeFile(path, options);

			//首先判断图片最大边是否超过maxValue
			if (Math.max(width, height) <= maxValue) {
				//直接返回原图
				return bitmap;
			} else {
				//如果大于maxValue，进行压缩处理
				float scale = ((float) maxValue) / Math.max(width, height);
				Matrix matrix = new Matrix();
				matrix.postScale(scale, scale);
				Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
				//回收原图
				bitmap.recycle();
				return resultBitmap;

			}
		}
		return null;
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		SelectPhotoUtil.getInstance().onActivityResult(requestCode, resultCode, data);
	}

	public void scaleImageOld(View view) {
		if (mUri != null) {
			mImageViewResultOld.setImageBitmap(BitmapUtils.maxWHBitmap(mUri, 1000));
		}
	}

	public void scaleImageNew(View view) {
		if (mUri != null) {
			mImageViewResultNew.setImageBitmap(maxWHBitmap(mUri, 1000));
		}
	}
}
